﻿using BLL;
using Model;
using System;

namespace Thksoft.Module.ParameterSettings
{
    public partial class ChangeUserPassword : BaseUI.BasePage//BasePage
    {
        private Sys_UserBLL sub = new Sys_UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUserCode.Text = lognUser.USER_LOGINNAME;
                txtUserName.Text = lognUser.USER_NAME.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserCode.Text))
            {
                MessageBox.Show(Page, "登录名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show(Page, "用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtOldPassword.Text))
            {
                MessageBox.Show(Page, "原密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtNewPassword.Text))
            {
                MessageBox.Show(Page, "新密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtConfirmPassword.Text))
            {
                MessageBox.Show(Page, "确认密码不能为空");
                return;
            }
            if (txtNewPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show(Page, "确认密码与新密码不同");
                return;
            }

            if (txtNewPassword.Text.IndexOf(" ") == -1 && txtConfirmPassword.Text.IndexOf(" ") == -1)
            {
                if (txtOldPassword.Text.Trim() != txtNewPassword.Text.Trim())
                {
                    string oldPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtOldPassword.Text.Trim(), "MD5");
                    string newPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text.Trim(), "MD5");

                    Sys_User model = sub.GetInfoByLogin(txtUserCode.Text.Trim().ToUpper());

                    if (model == null)
                    {
                        MessageBox.Show(this, "不存在该用户");
                        return;
                    }

                    if (oldPassword != model.USER_PWD)
                    {
                        MessageBox.Show(Page, "原密码错误");
                        return;
                    }
                    model.USER_PWD = newPassword;

                    int count = sub.UPDATE(model, lognUser.USER_NAME);
                    if (count > 0)
                    {
                        MessageBox.Show(Page, "保存成功");
                        return;
                    }
                    else
                    {
                        MessageBox.Show(Page, "保存失败");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show(Page, "新密码与旧密码不能相同！");
                    return;
                }
            }
            else
            {
                MessageBox.Show(Page, "新密码或确认密码不能为空字符串！");
                return;
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            //Response.Redirect("/Right.aspx");
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }
    }
}